﻿using UnityEngine;
using System.Collections;

public class AsteroidDestroy : MonoBehaviour {
	
	public int countCol= 0;
	public Transform ExplosionPrefab;
	GameObject asteroidSpawn;
	AsteroidField asteroidField;
	float distance;
	
	
	void Start()
	{
		asteroidSpawn = GameObject.Find("AsteroidSpawn");
		asteroidField = asteroidSpawn.GetComponent("AsteroidField") as AsteroidField;
		distance = Vector3.Distance(asteroidSpawn.transform.position, transform.position);
		if(distance > 2740) {
			Color color = new Color(1,1,1, (3000-distance)/255.0f);
			renderer.material.SetColor("_Color", color);
		}
		
		
	}
	void OnCollisionEnter (Collision col) {
		
		
			countCol++;
		Debug.Log(countCol);
		if(countCol >=8)
		{
			//transform.renderer.bounds.center um explosion in der mitte zu spawnen
			Instantiate(ExplosionPrefab, transform.renderer.bounds.center, Quaternion.identity);
			asteroidField.lowerCount();
			Destroy(gameObject);
		}
	
	}
	
	void Update()
	{
		distance = Vector3.Distance(asteroidSpawn.transform.position, transform.position);
		if(distance > 3500)
		{
			
			asteroidField.lowerCount();
			Destroy(gameObject);
		}
		if(distance > 2740) {
			Color color = new Color(1,1,1, (3000-distance)/255.0f);
			renderer.material.SetColor("_Color", color);
		}
		
	}
}
